package com.freecloud.framework.utils;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * properties util
 */
public class PropertiesUtil {
	private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
	private static final String file_name = "system-common.properties";

	/**
	 * load properties
	 * @param propertyFileName
	 * @return
	 */
	public static Properties loadProperties(String propertyFileName) {
		Properties prop = new Properties();
        InputStream  in = null;
		try {
            InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
			prop.load(is);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
		return prop;
	}

	public static String getString(String key) {
		Properties prop = loadProperties(file_name);
		if (prop!=null) {
			return prop.getProperty(key);
		}
		return null;
	}

	/**
	 * 验证上传跨域安全域名检测，
	 * @param sourceUrl  为空则返回false
	 * @return
	 */
	public static boolean checkWhiteURL(String sourceUrl)
	{
		if(StrUtil.isEmpty (sourceUrl))
		{
			return false;
		}

		String whitUrlStr = PropertiesUtil.getString("white.url");

		if(StrUtil.isEmpty (whitUrlStr))
		{
			return false;
		}

		String[] whitUrlArr = whitUrlStr.split (",");

		for(String whitUrl : whitUrlArr)
		{
			if(sourceUrl.indexOf (whitUrl) > 0)
			{
				return true;
			}
		}
		return false;
	}
	
	public static void main(String[] args) {
		System.out.println(getString("xxl.job.login.username"));
	}

}
